package cn.apestar.common.inteceptor;


import cn.apestar.util.AuthUtil;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.support.HttpRequestWrapper;

import java.io.IOException;

import static java.util.Collections.singletonList;

public class AcceptHeaderHttpRequestInterceptor implements ClientHttpRequestInterceptor {

  public AcceptHeaderHttpRequestInterceptor() {

  }

  @Override
  public ClientHttpResponse intercept(HttpRequest request, byte[] body,
                                      ClientHttpRequestExecution execution) throws IOException {

    HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
    requestWrapper.getHeaders().setContentType(type);
    requestWrapper.getHeaders().add("Accept", MediaType.APPLICATION_JSON.toString());
    AuthUtil.putAuth(requestWrapper.getHeaders());
    return execution.execute(requestWrapper, body);
  }
}